class CollectionsController < ApplicationController
  # GET /collections
  # GET /collections.json
  def index
    @collections = Collection.all
    @user = current_user

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @collections }
    end
  end

  # GET /collections/1
  # GET /collections/1.json
  def show
    destroy_collection_session
    @collection = Collection.find(params[:id])
    @collection_ratings = CollectionRating.joins(:collection).where("collection_id = ? ", @collection.id )
    @books = Book.find_all_by_collection_id(@collection.id)
    session[:collection_id] = @collection.id

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @collection }
    end
  end

  # GET /collections/new
  # GET /collections/new.json
  def new

    if admin?

      @collection = Collection.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render :json => @collection }
      end

    else
      flash.now.alert = "Please, log as admin to manage collections"
      respond_to do |format|
        format.html { render "home/index"}
      end
    return
    end
  end

  # GET /collections/1/edit
  def edit
    if admin?
    @collection = Collection.find(params[:id])
    else
            flash.now.alert = "Please, log as admin to manage collections"
      respond_to do |format|
        format.html { render "home/index"}  
      end
      return
    end
  end

  # POST /collections
  # POST /collections.json
  def create

    if admin?

      @collection = Collection.new(params[:collection])

      respond_to do |format|
        if @collection.save
          format.html { redirect_to @collection, :notice => 'Collection was successfully created.' }
          format.json { render :json => @collection, :status => :created, :location => @collection }
        else
          format.html { render :action => "new" }
          format.json { render :json => @collection.errors, :status => :unprocessable_entity }
        end
      end
    else

      flash.now.alert = "Please, log as admin to manage collections"
      respond_to do |format|
        format.html { render "home/index"}
      end
    return

    end
  end

  # PUT /collections/1
  # PUT /collections/1.json
  def update

    if admin?

      @collection = Collection.find(params[:id])

      respond_to do |format|
        if @collection.update_attributes(params[:collection])
          format.html { redirect_to @collection, :notice => 'Collection was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render :action => "edit" }
          format.json { render :json => @collection.errors, :status => :unprocessable_entity }
        end
      end

    else
      flash.now.alert = "Please, log as admin to manage collections"
      respond_to do |format|
        format.html { render "home/index"}
      end
    return
    end

  end

  # DELETE /collections/1
  # DELETE /collections/1.json
  def destroy

    if admin?
      @collection = Collection.find(params[:id])
      @collection.destroy

      respond_to do |format|
        format.html { redirect_to collections_url }
        format.json { head :no_content }
      end
    else
      flash.now.alert = "Please, log as admin to manage collections"
      respond_to do |format|
        format.html { render "home/index"}
      end
    return
    end
  end
end
